// pages/add/add.
let app=getApp()
const timer= require('../../utils/timer.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num:2,
    canshow:true,
    canadd:true,
    title: "",
    content1:'',
    content2:'',
    content:'',
    showLogin:true,
    showLogin1:true,
    pics:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
 
 
  },
  toMore(){
    console.log('more')
    wx.navigateTo({
      url:'../../more/newvote/add'
    })
  },
  previewImage(t){
    let e=t.currentTarget.dataset.url;
    if(!e){return}
    wx.previewImage({
      current: e,
      urls: [ e ]
  });
  },
  upimg(e){
    var that=this
    if(this.data.canadd==false){ 
      wx.showToast({
        title:'请等待',
        icon:'none'
      })
      return
      }
    wx.showLoading({
      title: '上传中...',
    })
    this.setData({
      canadd:false
    })
    wx.chooseImage({
      count: 1,
      success: function (res) {
        console.log(res)
        var tempFilePaths = res.tempFilePaths[0]
        var file;
        var uid = wx.getStorageSync('uid');
        let day=timer.formatTime(new Date());
        var randname=uid+'_'+day+'_vote.jpg';
        
   
        file = wx.Bmob.File(randname, tempFilePaths);
        file.imgSecCheck().then(res => {
          console.log("upimg=")
           console.log(res)
          if(res&&res[0].msg=='ok'){
             var file2= wx.Bmob.File(randname, tempFilePaths);
            file2.save().then(res=>{
           wx.hideLoading();
        console.log(res)
          that.setData({
            pics:res,
            canadd:true
          })
            })
          
        }else{
            wx.hideLoading();
            that.setData({
               canadd:true
            })
        }
        }).catch(err=>{
            console.log(err);
            wx.hideLoading()
            that.setData({
              canadd:true
            })
            wx.showToast({
              title:'图片违规',
              icon:'none'
            })
        })
       

      },
      fail(){
      that.setData({
        canadd:true
      })
      wx.hideLoading()
      }
    })

    setTimeout(function(res){
  that.setData({
        canadd:true
      })
    },800)
  },
  closeImages(e){
    this.setData({
      pics:[]
    })
  },
  updateUser(uid,sex,uimg,username){
    var param = {
      'id': uid,
      'sex': sex,
      'avatarurl': uimg,
      'nickName':username
    }
    wx.Als.bmobFunc('vote_user', 'update', param).then(res => {
      
      if (res) {
    
       
      }
    })
  },
 formSubmit: function(e) {
  console.log(e)
  let that=this
  let title = e.detail.value.title;
  let content1=e.detail.value.content1;
  let content2=e.detail.value.content2;
  let uid=wx.getStorageSync('uid');
  if(!uid||!this.data.canshow){
    console.log("不能用") 
    return
  }
  if ("" == e.detail.value.title) return wx.showToast({
      title: "标题不能为空",
      icon: "none",
      duration: 1e3
  })
  if ("" == content1||""==content2) return wx.showToast({
    title: "选项不能为空",
    icon: "none",
    duration: 1e3
})
var img='';
if(this.data.pics.length>0){
  img=this.data.pics[0].url
}

this.setData({
  canshow:false
})
this.add_vote(uid,title,content1,content2,img)

setTimeout(function(res){
  that.setData({
    canshow:true
  })
},1200);

 },
 getinfo(e){
  console.log(e);
  console.log("====")
 

  console.log(e.detail.errMsg)
  let that=this;
  var uid=wx.getStorageSync('uid')
  if (uid&&e.detail.userInfo) {
    console.log("tiaozhuan1")
    let userInfo = e.detail.userInfo
    let username = userInfo.nickName

    let sex = userInfo.gender
    let uimg = userInfo.avatarUrl
  
  
    wx.setStorageSync('sex', sex)
    wx.setStorageSync('myname', username)
    wx.setStorageSync('nickName', username);
    wx.setStorageSync('uimg', uimg)
    wx.setStorageSync('myurl', uimg)
    console.log("tiaozhuan3")
    this.updateUser(uid,sex,uimg,username)
    this.setData({
      showLogin:false
    })
 
  
  

  }
},
 add_vote(uid,title,content1,content2,img=''){
    let that=this;
    var nick_name=wx.getStorageSync('nickName');
    var avatar=wx.getStorageSync('uimg');
    var show=app.config.showCaicai?true:false;
    var level=wx.getStorageSync('level');
  
    let param={
      uid:uid,
      title:title,
      content1:content1,
      content2:content2,
      group_count:0,
      vote_content1:0,
      vote_content2:0,
      idlist:[],
      nick_name:nick_name,
      avatar:avatar,
      show:show,
      level:Number(level),
      desc_content:img
     
      
    }
   
    let mycontent=title+content1+content2
    
    wx.Bmob.checkMsg(mycontent).then(res => {
      console.log(res)
      if(res.msg=='ok'||res.msg=='OK'){
         wx.showLoading({
      title: '添加中...',
    })

wx.Als.bmobFunc('vote_list', 'add', param).then(res => {
      console.log(res)
      console.log(res)
      wx.hideLoading();
      if(res){
        that.setData({
          title:'',
          content:'',
          pics:[]
        })
        wx.showToast({
          title: '添加成功~',
        })

      }
    })

      }

  }).catch(err => {
      console.log(err)
      wx.showToast({
        title: '发布失败',
        icon:'none'
      })
  })
    
 },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var nickName=wx.getStorageSync('nickName')
     wx.hideLoading();
     this.setData({
        canadd:true
      })
    if(nickName){
      this.setData({
        showLogin:false
      })
    }else{
      this.setData({
        showLogin:true
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})